﻿using Acme.BookStore.RoomTable;
using Acme.BookStore.RoomTypess;
using HIS;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore.RoomNumbers
{
    [ApiExplorerSettings(GroupName ="v1")]
    public class RoomCodeInfoServices : ApplicationService, IRoomCodeInfoServices
    {
        public RoomCodeInfoServices(IRepository<RoomCodeInfo> RoomCodeInfoRepository,IRepository<RoomType> RoomTypeRepository)
        {
            this.RoomCodeInfoRepository = RoomCodeInfoRepository;
            this.RoomTypeRepository = RoomTypeRepository;
        }

        public IRepository<RoomCodeInfo> RoomCodeInfoRepository { get; }
        public IRepository<RoomType> RoomTypeRepository { get; }

        /// <summary>
        /// 添加房号
        /// </summary>
        /// <param name="roomCodeInfoeDTO">房号DTO</param>
        /// <returns></returns>
        [HttpPost("api/RoomCodeInfo")]
        public async Task<APIResult> RoomCodeInfoAdd(RoomCodeInfoDTO roomCodeInfoeDTO)
        {
            try
            {
                RoomCodeInfo roomCodeInfo=ObjectMapper.Map<RoomCodeInfoDTO, RoomCodeInfo>(roomCodeInfoeDTO);
                await RoomCodeInfoRepository.InsertAsync(roomCodeInfo);
                if (roomCodeInfo==null)
                {
                    return new APIResult()
                    {
                        Code = CodeEnum.error,
                        Message = "添加失败"
                    };
                }
                return new APIResult()
                {
                    Code = CodeEnum.success,
                    Message = "房号添加成功"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除房号
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        [HttpDelete("api/RoomCodeInfo")]
        public async Task<APIResult> RoomCodeInfoDelete(string Ids)
        {
            try
            {
                var idList = Ids.Split(',').Select(x => Convert.ToInt32(x)).ToList();
                var roomTypes = await RoomCodeInfoRepository.GetListAsync(x => idList.Contains(x.Id));

                if (roomTypes == null || !roomTypes.Any())
                {
                    return new APIResult()
                    {
                        Code = CodeEnum.error,
                        Message = "删除失败，未找到指定的房型"
                    };
                }

                foreach (var roomType in roomTypes)
                {
                    await RoomCodeInfoRepository.DeleteAsync(roomType);
                }

                return new APIResult()
                {
                    Code = CodeEnum.success,
                    Message = "删除成功"
                };
            }
            catch (Exception ex)
            {
                return new APIResult()
                {
                    Code = CodeEnum.error,
                    Message = $"删除失败: {ex.Message}"
                };
            }
        }
        /// <summary>
        /// 查询房号
        /// </summary>
        /// <param name="RoomTypeId"></param>
        /// <returns></returns>
        [HttpGet("api/RoomCodeInfo")]
        public async Task<APIResult<List<RoomCodeInfoDTO>>> RoomCodeInfoShow(int RoomTypeId, bool? status, string RoomCode, string? RoomName)
        {
            try
            {
                var roomtype= await RoomTypeRepository.GetListAsync();
                var roomcode= await RoomCodeInfoRepository.GetListAsync();
                var list = from a in roomcode
                           join b in roomtype on a.RoomTypeId equals b.Id
                           select new RoomCodeInfoDTO
                           {
                               Id = a.Id,
                               RoomCodeSort =a.RoomCodeSort,
                               RoomCodeMemo=a.RoomCodeMemo,
                               RoomCode = a.RoomCode,
                               RoomStatus = a.RoomStatus,
                               RoomTypeId = a.RoomTypeId,
                               RoomTypeName = b.RoomName
                           };
                if (RoomTypeId != 0)
                {
                    list = list.Where(x => x.RoomTypeId == RoomTypeId);
                }
                if (status ==true)
                {
                    list = list.Where(x => x.RoomStatus == true);
                }
                if (status == false)
                {
                    list = list.Where(x => x.RoomStatus == false);
                }
                if (!string.IsNullOrEmpty(RoomCode))
                {
                    list = list.Where(x => x.RoomCode == RoomCode);
                } if (!string.IsNullOrEmpty(RoomName))
                {
                    list = list.Where(x => x.RoomTypeName == RoomName);
                }
                
                return new APIResult<List<RoomCodeInfoDTO>>()
                {
                    Code = CodeEnum.success,
                    Message = "查询成功",
                    Data = list.ToList()
                };
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 删除单个房号
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete("api/RoomCodeInfoSingle")]
        public async Task<APIResult> RoomCodeInfoSingleDelete(int Id)
        {
            try
            {
                var roomcode = await RoomCodeInfoRepository.FirstOrDefaultAsync(x => x.Id == Id);
                if (roomcode == null)
                {
                    return new APIResult()
                    {
                        Code = CodeEnum.error,
                        Message = "删除失败，未找到指定的房型"
                    };
                }
                await RoomCodeInfoRepository.DeleteAsync(roomcode);
                return new APIResult()
                {
                    Code = CodeEnum.success,
                    Message = "删除成功"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改单个房号状态
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPut("api/RoomCodeInfoSingleStatus")]
        public async Task<APIResult> RoomCodeInfoSingleUpdateStatus(int Id)
        {
            try
            {
                var roomcode = await RoomCodeInfoRepository.FirstOrDefaultAsync(x => x.Id == Id);
                if (roomcode == null)
                {
                    return new APIResult()
                    {
                        Code = CodeEnum.error,
                        Message = "修改失败，未找到指定的房型"
                    };
                }
                roomcode.RoomStatus = !roomcode.RoomStatus;
                await RoomCodeInfoRepository.UpdateAsync(roomcode);
                return new APIResult()
                {
                    Code = CodeEnum.success,
                    Message = "修改成功"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 修改房号
        /// </summary>
        /// <param name="roomCodeInfoUpdateDTO"></param>
        /// <returns></returns>
        [HttpPut("api/RoomCodeInfo")]
        public async Task<APIResult> RoomCodeInfoUpdate(RoomCodeInfoUpdateDTO roomCodeInfoUpdateDTO)
        {
            try
            {
                var roomType = await RoomCodeInfoRepository.FirstOrDefaultAsync(x => x.Id == roomCodeInfoUpdateDTO.Id);
                if (roomType == null)
                {
                    return new APIResult()
                    {
                        Code = CodeEnum.error,
                        Message = "房型未找到"
                    };
                }

                ObjectMapper.Map(roomCodeInfoUpdateDTO, roomType);
                await RoomCodeInfoRepository.UpdateAsync(roomType);

                return new APIResult()
                {
                    Code = CodeEnum.success,
                    Message = "房型修改成功"
                };
            }
            catch (Exception ex)
            {
                return new APIResult()
                {
                    Code = CodeEnum.error,
                    Message = $"修改失败: {ex.Message}"
                };
            }
        }
        /// <summary>
        /// 修改房号状态
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        [HttpPut("api/RoomCodeInfoStatus")]
        public async Task<APIResult> RoomCodeInfoUpdateStatus(string Ids)
        {
            try
            {
                var idList = Ids.Split(',').Select(x => Convert.ToInt32(x)).ToList();
                var roomTypes = await RoomCodeInfoRepository.GetListAsync(x => idList.Contains(x.Id));

                if (roomTypes == null || !roomTypes.Any())
                {
                    return new APIResult()
                    {
                        Code = CodeEnum.error,
                        Message = "修改失败，未找到指定的房型"
                    };
                }

                foreach (var roomType in roomTypes)
                {
                    roomType.RoomStatus = false;
                    await RoomCodeInfoRepository.UpdateAsync(roomType);
                }

                return new APIResult()
                {
                    Code = CodeEnum.success,
                    Message = "修改成功"
                };
            }
            catch (Exception ex)
            {
                return new APIResult()
                {
                    Code = CodeEnum.error,
                    Message = $"修改失败: {ex.Message}"
                };
            }
        }
    }
}
